package com.qingzhuge.resource.entity;


import com.alibaba.fastjson.annotation.JSONField;
import com.google.common.collect.Lists;
import com.qingzhuge.common.enums.EResourceType;
import com.qingzhuge.entity.BaseModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import javax.persistence.*;
import java.util.List;

/**
 * @author : zero.xiao
 * @description : 菜单管理
 * @date :2018/6/8 0008 下午 15:00
 * @modified :
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Entity
@Table(name = "sys_resource")
public class SysResource extends BaseModel {
    private static final long serialVersionUID = 7297337976864115243L;
    /**
     * 父菜单ID，一级菜单为0
     */
    @ManyToOne(fetch = FetchType.EAGER)
    private SysResource parent;

    @OneToMany(mappedBy = "parent", fetch = FetchType.EAGER)
    @JSONField(serialize = false)
    private List<SysResource> children = Lists.newArrayList();
    /**
     * 菜单URL
     */
    private String url;
    /**
     * 显示菜单
     */
    private Boolean display;
    /**
     * 请求类型
     */
    private String requestMethod;

    /**
     * 权限(多个用逗号分隔，如：user:list,user:create)
     */
    private String permissions;
    /** 授予角色可以访问，多个角色用逗号分隔*/
    private String role;
    /** 授予授权可以访问，多个授权用逗号分隔*/
    private String authority;
    /**
     * 类型
     */
    private EResourceType type;
    /**
     * 菜单图标
     */
    private String icon;
    /**
     * 排序
     */
    private Integer orderNum;
    /**
     * ztree属性
     */
    @Transient
    private Boolean open;
    @Transient
    private List<?> list;
    @Transient
    private Integer leaf = 1;
    @Transient
    private String typeName;
    @Transient
    private String permissionText;


    public SysResource() {
    }

    public SysResource(String name) {
        super(name);
    }

    public SysResource(Long id) {
        super(id);
    }

    public SysResource(String name, Long id) {
        super(name, id);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
    }
}
